<?php namespace sacvoyage\domain;

require_once('User.php');
require_once('Resource.php');
require_once('Property.php');
require_once('Collection.php');

require_once('sacvoyage/app/FileApplication.php');
use \sacvoyage\app\FileApplication;

require_once('sacvoyage/app/LogoutApplication.php');
use \sacvoyage\app\LogoutApplication;

require_once('sacvoyage/app/CustomApplication.php');
use \sacvoyage\app\CustomApplication;

class Share implements Resource {
	
	public function __construct(Property $property, $config) {
		$this->_property=$property;
		$this->_config=$config;
	}
	
	public function getOwner() {
		return $this->_property->getOwner();
	}
	
	private function _getConfig() {
		return $this->_config;	
	}
	
	public function getApplicationNames() {
		if($this->_config 
		&& isset($this->_config->applications)
		&& is_object($this->_config->applications)) {
			return $this->_config->applications;
		} else {
			return array();
		}
	}
	
	public function getApplication($name) {
		if(in_array($name, $this->getApplicationNames())) {
			$builtin=BuiltinApplication::createApplication($name);
			if($builtin) {
				return $builtin;
			}
			$config=$this->getOwner()->getProperty()
					->getCollection('cfg.app')->read($name);
			if($config) {
				return new CustomApplication($this, $config);
			}
		}
		return null;
	}
	
	public function getCollectionNames() {
		if(isset($this->_getConfig()->permissions)) {
			$this->_collectionNames=
			get_object_vars($this->_getConfig()->permissions);
		} else {
			$this->_collectionNames=array();
		}
	}
	
	public function getCollection($name) {
		if(isset($this->_getConfig()->permissions->$name)
		&& in_array(
			$this->_getConfig()->permissions->$name, 
			array('full', 'write', 'read'))
		) {
			return $this->_property->getCollection(
				$name, 
				$this->_getConfig()->permissions->$name
			);
		}
		return null;
	}
	
	private $_property;
	private $_config;
	
}

?>
